package com.youlai.boot.modules.tp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.youlai.boot.modules.tp.entity.DailySalesReport;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 每日销售报表Mapper接口
 * 
 * @author Jason
 * @since 2025-06-04
 */
@Mapper
@org.springframework.stereotype.Repository("tpDailySalesReportMapper")
public interface DailySalesReportMapper extends BaseMapper<DailySalesReport> {

    /**
     * 获取指定日期范围的销售数据
     * 
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 销售数据列表
     */
    @Select("SELECT * FROM daily_sales_report WHERE report_date BETWEEN #{startDate} AND #{endDate} AND is_deleted = 0 ORDER BY report_date ASC")
    List<DailySalesReport> getSalesDataByDateRange(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    /**
     * 获取最新的销售报表
     * 
     * @return 最新销售报表
     */
    @Select("SELECT * FROM daily_sales_report WHERE is_deleted = 0 ORDER BY report_date DESC LIMIT 1")
    DailySalesReport getLatestSalesReport();

    /**
     * 获取月度销售汇总数据
     * 
     * @param year 年份
     * @param month 月份
     * @return 月度汇总数据
     */
    @Select("SELECT " +
            "SUM(current_sales_amount) as totalSalesAmount, " +
            "AVG(conversion_rate) as avgConversionRate, " +
            "AVG(average_transaction_value) as avgTransactionValue, " +
            "SUM(total_sales_quantity) as totalSalesQuantity, " +
            "MAX(monthly_sales_target) as monthlySalesTarget " +
            "FROM daily_sales_report " +
            "WHERE YEAR(report_date) = #{year} AND MONTH(report_date) = #{month} AND is_deleted = 0")
    Map<String, Object> getMonthlySalesSummary(@Param("year") int year, @Param("month") int month);

    /**
     * 获取销售趋势数据
     * 
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 销售趋势数据
     */
    @Select("SELECT " +
            "report_date as date, " +
            "current_sales_amount as gmv, " +
            "total_sales_quantity as orders " +
            "FROM daily_sales_report " +
            "WHERE report_date BETWEEN #{startDate} AND #{endDate} AND is_deleted = 0 " +
            "ORDER BY report_date ASC")
    List<Map<String, Object>> getSalesTrendData(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    /**
     * 获取今日销售指标
     * 
     * @param today 今日日期
     * @return 今日销售指标
     */
    @Select("SELECT " +
            "current_sales_amount as todayGMV, " +
            "conversion_rate, " +
            "average_transaction_value, " +
            "total_sales_quantity as todayOrders, " +
            "sales_progress, " +
            "monthly_sales_target " +
            "FROM daily_sales_report " +
            "WHERE report_date = #{today} AND is_deleted = 0")
    Map<String, Object> getTodayMetrics(@Param("today") LocalDate today);

    /**
     * 计算增长率
     * 
     * @param currentDate 当前日期
     * @param compareDate 对比日期
     * @return 增长率数据
     */
    Map<String, Object> calculateGrowthRates(@Param("currentDate") LocalDate currentDate, @Param("compareDate") LocalDate compareDate);
} 